package kr.saeildamil.androidforstudy2.ch20.common;

import kr.saeildamil.androidforstudy2.R;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class CounterFragment03 extends Fragment {
	
	public static CounterFragment03 newInstance(int start){
		CounterFragment03 cf03 = new CounterFragment03();
		
		Bundle args = new Bundle();
		args.putInt("start", start);
		cf03.setArguments(args);
		
		return cf03;
	}	// 생성자 대신으로..(자기자신을 리턴);
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View root = inflater.inflate(R.layout.ch20_counterfragment, container, false);
		Button increaseBtn = (Button) root.findViewById(R.id.ch20_counterfragment_increasebtn);
		final TextView textCount = (TextView) root.findViewById(R.id.ch20_counterfragment_countertv); 
		
		int start = 0;
		
		Bundle args = getArguments();
		
		if(args!=null){
			start = args.getInt("start");
		}
		textCount.setText(Integer.toString(start));
		increaseBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int count = Integer.parseInt(textCount.getText().toString());
				textCount.setText(Integer.toString(count+1));
			}
		});
		return root;
	}
	
}
